package com.xy.breakfast.mapper;


import com.xy.breakfast.entity.Foods;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.awt.print.Book;
import java.util.List;
import java.util.Map;

//商品数据封装
@Mapper
public interface FoodsMapper extends BaseMapper<Foods>{

    @Select("select * from foods where id=#{id}")
    @Results({
            @Result(property = "typesId", column = "types_id", jdbcType = JdbcType.INTEGER),
            @Result(property = "createTime", column = "create_time", jdbcType = JdbcType.INTEGER)
    })
    public Foods getById(int id);

    public void save(Foods t);

    public void deleteById(int id);

    @Update({"UPDATE foods " +
            "SET types_id = #{typesId}," +
            "name = #{name}," +
            "pic = #{pic}," +
            "price = #{price}," +
            "num = #{num}," +
            "create_time = #{createTime}," +
            "detail = #{detail} " +
            "WHERE id = #{id}"})
    public void update(Foods t);

    //食品过多，显示20个数据使用
    @Select("select * from foods order by foods.num limit 20")
    @Results({
            @Result(property = "typesId", column = "types_id", jdbcType = JdbcType.INTEGER),
            @Result(property = "createTime", column = "create_time", jdbcType = JdbcType.INTEGER)
    })
    public List<Foods> getAll();

    @Select("select * from foods where types_id=#{typesId}")
    @Results({
            @Result(property = "typesId", column = "types_id", jdbcType = JdbcType.INTEGER),
            @Result(property = "createTime", column = "create_time", jdbcType = JdbcType.INTEGER)
    })
    //按照类型获取食品
    public List<Foods>getByTypesId(int typesId);

    public List<Foods> getPage(int start,int end);

    @Select("select foods.name,sum(foods.price*orders.num) as value\n" +
            "            from orders,foods\n" +
            "            where orders.foods_id=foods.id and\n" +
            "            day(orders.create_time)=day(now()) and\n" +
            "            MONTH(orders.create_time)=MONTH(now()) and\n" +
            "            YEAR(orders.create_time)=YEAR(now()) group by  foods_id")
    public List<Map<String,Object>>getFoodsSalePrice();

    //当天不同类型的销售额
    @Select("select types_id as x, count(id) as y\n" +
            "            from foods\n" +
            "            where day(create_time) = day(now())\n" +
            "            group by types_id;")
    public List<Map<String,String>> getFoodsByTypesIdDay();


//    不同类型销售比例图
    @Select("SELECT  SUM(f.num) AS value,t.name AS name\n" +
            "FROM foods f\n" +
            "\n" +
            "         JOIN types t ON f.types_id = t.id\n" +
            "where day(f.create_time)=day(now())\n" +
            "GROUP BY f.types_id;")
    public List<Map<String,Object>> getFoodsByNum();

    //按名字查询图书
    @Select("select * from foods where name=#{name}")
    public  List<Foods> getBooksByName(@Param("name") String name);

}
